<?php
// $Id: phpCR.library.php 73 2007-02-12 03:45:43Z tswicegood $

/**
 * This file contains {@link phpCR} which is part of the PHP Content Repository
 * (phpCR), and is licensed under the Apache License, Version 2.0.
 *
 * @author Travis Swicegood <travis@domain51.net>
 * @copyright PHP Code Copyright &copy; 2004-2005, Domain51, United States
 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, 
 *      Version 2.0
 * @package phpContentRepository
 */


/**
 * This class provides a method for loading files, controlling variables, etc.,
 * within the phpContentRepository interfaces.
 * 
 * @author Travis Swicegood <travis@domain51.net>
 * @copyright Copyright &copy; 2004-2005, Domain51, United States
 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, 
 *      Version 2.0
 * @package phpContentRepository
 */
class phpCR
{
   /**
    * The level of phpCR level of implementation.
    *
    * @var int
    * @see getLevel(), setLevel()
    */
    static private $_level = 1;
    
    
   /**
    * The root path of this implementation of phpCR
    *
    * @var string
    * @see getLevel(), setLevel(), __construct()
    */
    static private $_path = null;
    
    
   /**
    * Handle initialization
    */
    private function __construct()
    {
    }
    
    
   /**
    * Returns the current implementation level for phpCR
    *
    * @return int
    */
    static public function getLevel()
    {
        return self::$_level;
    }
    
    
   /**
    * Returns the current path of the phpCR files
    *
    * @return string
    */
    static public function getPath()
    {
        if (is_null(self::$_path)) {
            self::setPath(dirname(__FILE__));
        }
        
        return self::$_path;
    }
    
    
   /**
    * Loads a phpCR abstract.
    *
    * @param string
    *   The abstract name.
    * @param string|null 
    *   The path to load the interface from.  This can not contain a trailing
    *   slash. (Optional)
    */
    static public function loadAbstract($name, $path = null)
    {
        assert('is_null($path) || substr($path, -1) != DIRECTORY_SEPARATOR');
        
        $file = self::getPath() . "{$path}/{$name}.abstract.php";
        assert('file_exists($file)');
        
        require_once($file);
    }
    
    
   /**
    * Loads a phpCR class.
    *
    * @param string 
    *   The interface name.
    * @param string|null 
    *   The path to load the interface from.  This can not contain a trailing
    *   slash. (Optional)
    */
    static public function loadClass($name, $path = null)
    {
        assert('is_null($path) || substr($path, -1) != DIRECTORY_SEPARATOR');
        
        $file = self::getPath() . "{$path}/{$name}.class.php";
        assert('file_exists($file)');
        
        require_once($file);
    }
    
    
   /**
    * Loads a phpCR exception.
    *
    * This function will look for a previously declared version of this file
    * before loading.  This allows for the implementation of your own exceptions
    * in place of the provided defaults.  Important Note: This does
    * not check to insure that the implementation meets the JCR/phpCR 
    * specification. 
    *
    * @param string
    *   The exception name minus "Exception".  Example:
    *   
    *       phpCR::loadException('Login'); // Loads LoginException
    *       phpCR::loadException('Repository'); // Loads RepositoryException
    *   
    */
    static public function loadException($name)
    {
        $realName = $name . "Exception";
        if (class_exists($realName)) {
            // Returns silently if this exception already exists
            return;
        }
        
        $file = self::getPath() . "/exceptions/{$realName}.exception.php";
        
        require_once($file);
    }
    
    
   /** 
    * Loads a phpCR interface
    *
    * @param string 
    *   The interface name.
    * @param string|null 
    *   The path to load the interface from.  This can not contain a trailing
    *   slash. (Optional)
    */
    static public function loadInterface($name, $path = null)
    {
        assert('is_null($path) || substr($path, -1) != DIRECTORY_SEPARATOR');
        
        $file = self::getPath() . "{$path}/{$name}.interface.php";
        assert('file_exists($file)');
        
        require_once($file);
    }
    
    
    
    
   /**
    * Sets the level of implementation for phpCR
    *
    * @param int
    */
    static public function setLevel($level)
    {
        assert('is_int($level)');
        
        self::$_level = $level;
    }
    
    
   /**
    * Sets the path to look for all of the phpCR files.
    *
    * Note: This performs an {@link assert()} 
    * on the $path to insure it exists.  This can be turned off
    * by calling assert_options() with the appropriate setting.
    *
    * @param string
    */
    static public function setPath($path)
    {
        assert('is_string($path)');
        assert('file_exists($path) || is_dir($path)');
        
        self::$_path = $path;
    }
}


/**
 * Do some checking to see if any phpCR constants have been defined.  If they
 * have, adjust the appropriate settings.
 */
if (defined('_PHPCR_LEVEL')) {
    // Don't set unless _PHPCR_LEVEL is greater than 1
    if (_PHPCR_LEVEL > 1) {
        phpCR::setLevel(_PHPCR_LEVEL);
    }
}

if (defined('_PHPCR_PATH')) {
    phpCR::setPath(_PHPCR_PATH);
}

